<?php
namespace app\admin\controller;

use helper\form\Builder;

/**
 * Class Plugin
 * @package app\admin\controller
 * @property \app\admin\service\Plugin $service
 */
class Plugin extends Admin
{

	protected $noNeedLogin = ['*'];
	
	public function index()
	{
		if ( $this->request->isAjax() ){
		if($this->request->has('keyword','post')){
				$keyword = $this->request->post('keyword');
				$where = [];
			}else{
				$where = [];
			}
			return $this->service->getTableList($where);
		}
		return $this->fetch();
	}

	public function install($flag=''){
		$this->service->install($flag);
	}

	public function uninstall($flag=''){
		$this->service->uninstall($flag);
	}

	public function config($flag=null){
		if($this->request->isPost()){
			return $this->service->config($flag);
		}
        $plugin = $this->service->getModel()->where('flag',$flag)->findOrEmpty();
		if($plugin->isEmpty() || $plugin->is_install != 1){
		    return $this->error('插件不存在或者未安装');
        }
		$config = \app\admin\model\Config::where('group',$flag)->select();
		if(!$config->isEmpty()){
			$config = $config->toArray();
			$html = (new Builder($config))->getHtml();
		}else{
			$html = '';
		}
		return $this->fetch('',['html'=>$html]);
	}

	public function grid(){
		$this->assign('plugins',$this->service->grid());
		return $this->fetch();
	}

	public function info($flag=''){
		$this->assign('info',$this->service->info($flag));
		return $this->fetch();
	}
}
